package org.jasig.cas.util;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ResponseFilter implements Filter {
	private static ThreadLocal<ServletResponse> connectionHolder = new ThreadLocal<ServletResponse>();
	
	public void destroy() {
		
	}

	public void doFilter(ServletRequest servletrequest,
			ServletResponse servletresponse, FilterChain filterchain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)servletrequest;
		if(request.getServletPath().equals("/logout")){
			HttpServletResponse response = (HttpServletResponse)servletresponse;
			Cookie cookie = new Cookie("username", null); 
			cookie.setMaxAge(0);
			response.addCookie(cookie); 
			cookie = new Cookie("password", null); 
			cookie.setMaxAge(0);
			response.addCookie(cookie); 
		}else if(request.getServletPath().equals("/logoutnew")){
			HttpServletResponse response = (HttpServletResponse)servletresponse;
			Cookie cookie = new Cookie("username_admin", null); 
			cookie.setMaxAge(0);
			response.addCookie(cookie); 
			cookie = new Cookie("password_admin", null); 
			cookie.setMaxAge(0);
			response.addCookie(cookie); 
		}
		connectionHolder.set(servletresponse);
		filterchain.doFilter(servletrequest, servletresponse);
	}

	public void init(FilterConfig filterconfig) throws ServletException {
		
	}

	public static ServletResponse getResponse(){
		return connectionHolder.get();
	}


}
